<template>
    <div class="achievement-list-page">
        <el-card shadow="never" class="mt-4">
            <el-form :inline="true" @keyup.enter.native="onSubmit">
                <el-form-item label="学习路径（岗位角色/学习计划/专题）筛选">
                    <el-select
                        v-model="form.labelId"
                        clearable
                        @change="fetchData()">
                        <el-option
                            v-for="item in labelOptions"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="关键词" prop="name">
                    <el-input
                        v-model="form.name"
                        placeholder="搜索学习主题"
                        class="input-width"/>
                </el-form-item>
                <el-button type="primary" @click="onSubmit">搜索</el-button>
                <el-button type="primary" @click="onRestForm">重置</el-button>
            </el-form>
            <div class="mt-3" v-loading="loading">
                <el-table :data="data">
                    <el-table-column type="index"
                    :index="indexMethod"
                    align="center"
                    label="序号"
                    width="50"/>
                    <el-table-column label="名称" prop="name"></el-table-column>
                    <el-table-column label="学习路径（岗位角色/学习计划/专题）">
                        <template slot-scope="{row}">
                            <div v-for="(v, i) in row.labels" :key="i">
                                <el-tag class="m-1" v-if="v">{{ v }}</el-tag></div>
                        </template>
                    </el-table-column>
                    <el-table-column label="已上架节点数" prop="count" width="120"/>
                    <el-table-column label="状态" width="80">
                        <template slot-scope="{row}">
                            {{ row.status ? "上架" : "下架" }}
                        </template>
                    </el-table-column>
                    <el-table-column label="所属企业" prop="company"/>
                    <el-table-column label="创建时间">
                        <template slot-scope="{row}">
                            {{ formatTime(row.created_at) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="{row}">
                            <el-button type="text" @click="onEditClick(row)">编辑</el-button>
                            <el-button type="text" @click="onPublishClick(row)">
                                {{ row.status ? "下架" : "上架" }}</el-button>
                            <el-button
                                type="text"
                                @click="onDeleteClick(row)"
                                class="red-color">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div v-if="totalElements > 0" class="footer">
                <pagination
                ref="pagination"
                :currentPage="form.page"
                :pageSize="form.size"
                :handleCurrentChange="onCurrentChange"
                :handlePageSizeChange="onPageSizeChange"
                :totalElements="totalElements"
                ></pagination>
            </div>
        </el-card>
    </div>
</template>

<script>
import Pagination from '../../components/base/Pagination';
import TimeFormats from '../../helpers/TimeFormats';
import IndexNumber from '../../helpers/IndexNumber';

export default {
    components: {
        Pagination,
    },
    mixins: [require('../../mixins/CacheQuery').default],
    data() {
        return {
            loading: false,
            data: [],
            form: {
                page: 1,
                size: 50,
                name: '',
                labelId: '',
                filter: 'company',
            },
            totalElements: 0,
            labelOptions: [],
            listForm: {
                filterType: 'company',
            },
        };
    },
    filters: {
        labelFilter: function (value) {
            if (!value) return '';
            return value.join(', ');
        },
    },
    methods: {
        formatTime(timestamp) {
            return timestamp ? new TimeFormats().convertTime('', timestamp) : '';
        },
        onSubmit() {
            this.form.page = 1;
            this.fetchData();
        },
        onResetFilters() {
            this.$refs.form.resetFields();
        },
        onCurrentChange(val) {
            this.form.page = val;
            this.fetchData();
        },
        onPageSizeChange(val) {
            this.form.size = val;
            this.form.page = 1;
            this.fetchData();
        },
        fetchData() {
            this.loading = true;
            API.get('course', {params: this.form}).then((res) => {
                this.data = res.content;
                this.totalElements = res.pagination.total;
            }).finally(() => {
                this.loading = false;
            });
        },
        onEditClick(row) {
            if (row.status) {
                $ele.$message.error('先对标签下架后才进行编辑操作');
                return;
            }
            this.$router.push({
                name: 'CompanyCourseManage.Add',
                params: { id: row.id },
            });
        },
        onDeleteClick(row) {
            if (row.status) {
                $ele.$message.error('先对标签下架后才进行删除操作');
                return;
            }
            $ele.$confirm('是否删除此学习主题', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.loading = true;
                API.delete(`course/${row.id}`, {
                }).then(() => {
                    $ele.$message.success('删除成功');
                    this.fetchData();
                }).finally(() => {
                    this.loading = false;
                });
            }).catch(() => { });
        },
        onPublishClick(row) {
            const content = row.status ? '是否下架学习主题' : '是否上架学习主题';
            const requestStatus = !row.status;
            $ele.$confirm(content, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.loading = true;
                API.put(`course/${row.id}/${requestStatus}`).then(() => {
                    $ele.$message.success('操作成功');
                    this.fetchData();
                }).finally(() => {
                    this.loading = false;
                });
            }).catch(() => { });
        },
        fetchLabel() {
            API.get('label/list', {params: this.listForm}).then((res) => {
                this.labelOptions = res;
            });
        },
        onRestForm() {
            this.form.labelId = '';
            this.form.name = '';
            this.fetchData();
        },
        indexMethod(index) {
            return new IndexNumber()
                .getIndex(this.totalElements, this.form.page, this.form.size, index);
        },
    },
    mounted() {
        this.fetchData();
        this.fetchLabel();
    },
};
</script>

<style lang="scss">
.achievement-list-page {
    .filter {
        .input-width {
            max-width: 200px;
        }
    }
    .logo {
        width: 60px;
        height: 60px;
    }
    .footer {
        height: 44px;
        background: #f7f7f7;
        display: flex;
        justify-content: flex-end;
        align-items: center;
        padding-left: 20px;
        padding-right: 20px;
    }
    .image-slot {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100%;
        height: 100%;
        background: #f5f7fa;
        color: #909399;
    }
}
</style>
